'''
Description: Humanoid Robot with AI,large language model API
Copyright (c) 2024 APS
FilePath: 
version: 
Author: Humanoid Robot Team
Date: 2024-09-13 13:55:01
LastEditors: SY
LastEditTime: 2024-06-24 15:54:51
'''

print('importing llm api')


import os
import requests
import openai
from openai import OpenAI
from utils_py.utils_api_key import *
import base64

#图片转base64函数
def encode_image(image_path):
  with open(image_path, "rb") as image_file:
    return base64.b64encode(image_file.read()).decode('utf-8')
  
#输入图片路径
image_path = "/home/gr1p24my0062/vertu_workspace/humanoid-robot_v2/color.png"


def llm_yi(PROMPT='你从这张图片里看到了什么？'):
    '''
    lingyi-ai API
    '''
    
    API_BASE = "https://api.lingyiwanwu.com/v1"
    API_KEY = YI_KEY

    # MODEL = 'yi-large'
    # MODEL = 'yi-medium'
    # MODEL = 'yi-spark'
        #原图片转base64
    base64_image = encode_image(image_path)
    # 访问大模型API
    client = OpenAI(api_key=API_KEY, base_url=API_BASE)
    completion = client.chat.completions.create(

        model="yi-vision",

        messages=[

            {

            "role": "user",

            "content": [

                {

                "type": "text",

                "text": PROMPT

                },

                {

                "type": "image_url",

                "image_url": {

                    "url": f"data:image/jpeg;base64,{base64_image}"

                }

                },

            ]

            },

        ]

    )
    # completion = client.chat.completions.create(model=MODEL, messages=[{"role": "user", "content": PROMPT}])
    result = completion.choices[0].message.content.strip()
    print(result)
    return result


##嵌入chatgpt-4o,加入FastGPT知识库，支持多轮对话
def Vertu_AI(PROMPT='你好，你是谁？'):
    API_BASE = VERTUAPI_BASE
    API_KEY = VERTUAPI_KEY

    MODEL = 'gpt-4o'
    # MODEL = 'yi-medium'
    # MODEL = 'yi-spark'
    #原图片转base64
    base64_image = encode_image(image_path)

    url = API_BASE  # 替换为目标URL
    data = {
        "model": "gpt-4o",
        "chatId": "2",
        "messages": [
            {
                "role": "user",
                "content": PROMPT
            },
            {
                "type": "image_url",
                "image_url":{
                    "url": f"data:image/jpeg;base64,{base64_image}"
                }
            },
        ]
    }
    headers = {
        "Authorization": API_KEY
    }
    response = requests.post(url, json=data, headers=headers, timeout=600)
    print(response.text)
    json_data = response.json()
    result = json_data["choices"][0]["message"]["content"]
    return result

# Vertu_AI("图片里你看到了什么？")
# llm_yi()
# Deepseek多轮对话,最多支持3轮对话
def llm_deepseek(PROMPT='你好，你是谁？'):
    '''
    Deepseek LLM API
    '''
    client = OpenAI(api_key= DEEPSEEK_KEY, base_url= DEEPSEEK_BASE)

# Round 1
    messages = [{"role": "user", "content": PROMPT}]
    response = client.chat.completions.create(
        model="deepseek-chat",
        messages=messages
    )

    messages.append(response.choices[0].message)
    print(f"Messages Round 1: {messages}")
    
# Round 2
    messages.append({"role": "user", "content": PROMPT})
    response = client.chat.completions.create(
        model="deepseek-chat",
        messages=messages
    )

    messages.append(response.choices[0].message)
    print(f"Messages Round 2: {messages}")
# Round 3
    messages.append({"role": "user", "content": PROMPT})
    response = client.chat.completions.create(
        model="deepseek-chat",
        messages=messages
    )

    messages.append(response.choices[0].message)
    print(f"Messages Round 2: {messages}")
    return messages

# llm_deepseek()
